package com.servlet.uploaddownload;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oreilly.servlet.MultipartRequest;

@WebServlet("/Upload")
public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html");
		int maxPostSize = 10 * 1024 * 1024;
		MultipartRequest multi = new MultipartRequest(req, ".", maxPostSize);
		
		String userName = (String) req.getSession().getAttribute("userName");
		String fileRepository = "C:/usersjsp/";
		File path = new File(fileRepository);
		if (!path.exists()) {
			System.out.println(path.mkdir());
		}
		String userFolder = fileRepository + userName;
		path = new File(userFolder);
		if (!path.exists()) {
			path.mkdir();
		}
		
		File file = multi.getFile("fileUpload");
		if (file!=null) {
			file.renameTo(new File(userFolder, file.getName()));
			req.getRequestDispatcher("/ListDownload").forward(req, resp);
		}
	}
}
